/**
 * DateUtils.java
 * Created at 2017-12-8
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 * <p>ClassName: DateUtils</p>
 * <p>Description: Date工具类整合</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-12-8</p>
 */
public class DateUtils {

    public static String getWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String dayOfWeek;
        if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            dayOfWeek = "7";
        }else{
            dayOfWeek = String.valueOf(c.get(Calendar.DAY_OF_WEEK) - 1);
        }
        return dayOfWeek;
    }

    public static String getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return String.valueOf(hour);
    }
    
    public static String getCurrentTime(String parttern) {
        Date now = new Date();
        String current = getDateFormat(parttern).format(now);
        return current;
    }
    
    public static String getCurrentTime() {
        Date now = new Date();
        String current = getDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        return current;
    }
    
    
    /**
     * 获取SimpleDateFormat
     * @param parttern 日期格式
     * @return SimpleDateFormat对象
     * @throws RuntimeException 异常：非法日期格式
     */
    public static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
        return new SimpleDateFormat(parttern);
    }

    
    /**
     * 获取昨天凌晨时间
     * @return
     */
    public static Date getYesterdayStart () throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);

        date = calendar.getTime();

        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");

        StringBuffer time = new StringBuffer();

        String dateString  = format.format(date);

        time.append(dateString).append(" ").append("00:00:00");

        return sdf.parse(time.toString());

    }
    
    public static String getOneHoursAgoTime(Date date){
        Calendar cal = Calendar. getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY,  cal.get(Calendar.HOUR_OF_DAY) - 1);
        Date oneHourAgo = cal.getTime();
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String oneHourAgoStr = f.format(oneHourAgo);
        return oneHourAgoStr;
    }
    
    public static String getPastHoursAgoTime(Date date,int hour){
        Calendar cal = Calendar. getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY,  cal.get(Calendar.HOUR_OF_DAY) - hour);
        Date oneHourAgo = cal.getTime();
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String oneHourAgoStr = f.format(oneHourAgo);
        return oneHourAgoStr;
    }
    
    
    
    /**
     * 获取昨天凌晨时间
     * @return
     */
    public static String getYesterdayStartStr () throws ParseException {

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);

        date = calendar.getTime();

        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");

        StringBuffer time = new StringBuffer();

        String dateString  = format.format(date);

        time.append(dateString).append(" ").append("00:00:00");

        return time.toString();

    }
    
    /**
     * 获取昨天凌晨时间
     * @return
     */
    public static String getDayBeforeYesterdayStartStr () throws ParseException {

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -2);

        date = calendar.getTime();

        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");

        StringBuffer time = new StringBuffer();

        String dateString  = format.format(date);

        time.append(dateString).append(" ").append("00:00:00");

        return time.toString();

    }
    
    /**
     * 获取昨天最后时间
     * @return
     */
    public static Date getYesterdayEnd () throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);

        date = calendar.getTime();

        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");

        StringBuffer time = new StringBuffer();

        String dateString  = format.format(date);

        time.append(dateString).append(" ").append("23:59:59");

        return sdf.parse(time.toString());
    }
    
    /**
     * 获取昨天最后时间
     * @return
     */
    public static String getYesterdayEndStr () throws ParseException {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);

        date = calendar.getTime();

        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");

        StringBuffer time = new StringBuffer();

        String dateString  = format.format(date);

        time.append(dateString).append(" ").append("23:59:59");

        return time.toString();
    }
    
    /**
     * 获取昨天最后时间
     * @return
     */
    public static String getDayBeforeYesterdayEndStr () throws ParseException {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);

        date = calendar.getTime();

        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");

        StringBuffer time = new StringBuffer();

        String dateString  = format.format(date);

        time.append(dateString).append(" ").append("23:59:59");

        return time.toString();
    }
    
    /**
     * 
     * <p>Description: 获取上周开始时间</p>
     * @return
     */
    public static Date getBeginDayOfLastWeek() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }
    
    /**
     * 
     * <p>Description: 获取上周结束时间</p>
     * @return
     */
    public static Date getEndDayOfLastWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }
    
    /** 
     * 获取过去第几天的日期 
     * 
     * @param past 
     * @return 
     */  
    public static String getPastDate(int past) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
        Date today = calendar.getTime();  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String result = format.format(today);  
        return result;  
    }  
    
    /** 
     * 获取过去第几天的日期 
     * 
     * @param past 
     * @return 
     */  
    public static String getPastDate(int past,Date date) {  
        Calendar calendar = Calendar.getInstance();  
        if(date!=null) calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
        Date today = calendar.getTime();  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String result = format.format(today);  
        return result;  
    }  
    
    
    /**
     * 
     * <p>Description: 获取一天开始时间</p>
     * @param d
     * @return
     */
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }
    
    /**
     * 
     * <p>Description: 获取一天开始时间</p>
     * @param d
     * @return
     */
    public static String getDayStartStr(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        
        Date date = new Date();
        date = calendar.getTime();
        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");
        StringBuffer time = new StringBuffer();
        String dateString  = format.format(date);
        time.append(dateString).append(" ").append("00:00:00");
        return time.toString();
        
    }
    
    public static String getTomorrowBegin() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,1);
        date=calendar.getTime(); 
        String tomorrow = getDayStartStr(date);
        return tomorrow;
    }
    
    public static Date getNextMonthBegin() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() , 1,0,0,0);
        return calendar.getTime();
    }
    
    /**
     * 
     * <p>Description: 获取一天开始时间</p>
     * @param d
     * @return
     */
    public static String getDayEndStr(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date = new Date();
        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");
        StringBuffer time = new StringBuffer();
        String dateString  = format.format(date);
        time.append(dateString).append(" ").append("23:59:59");
        return time.toString();
        
    }
    
    
    /**
     * 
     * <p>Description: 获取一天最后时间</p>
     * @param d
     * @return
     */
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }
    
    /** 
     * 获取上个季度的开始
     * @return 
     */  
    public static Date getBeginOfLastQuarter() {  
        Calendar startCalendar = Calendar.getInstance();  
        startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);  
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);  
        setMinTime(startCalendar);  
        return startCalendar.getTime();  
    } 
    
    /** 
     * 获取上个季度的结束
     * @return 
     */  
    public static Date getEndOfLastQuarter() {  
        Calendar endCalendar = Calendar.getInstance();  
        endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);  
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
        setMaxTime(endCalendar);  
        return endCalendar.getTime();  
    } 
    
    public static Date getBeginOfLastYear() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(Calendar.YEAR, startCalendar.get(Calendar.YEAR)-1);
        startCalendar.set(Calendar.MONTH, 0);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);  
        setMinTime(startCalendar);  
        return startCalendar.getTime();  
    }
    
    public static Date getEndOfLastYear() {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.YEAR, endCalendar.get(Calendar.YEAR)-1);
        endCalendar.set(Calendar.MONTH, 11);
        endCalendar.set(Calendar.DAY_OF_MONTH, 31);  
        setMaxTime(endCalendar);  
        return endCalendar.getTime();  
    }
    
    
  
    public static Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        return getDayStartTime(calendar.getTime());
    }
    
    
    public static Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 2, day);
        return getDayEndTime(calendar.getTime());
    }
   
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }
    
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }
    
    private static void setMinTime(Calendar calendar){  
        calendar.set(Calendar.HOUR_OF_DAY, 0);  
        calendar.set(Calendar.MINUTE, 0);  
        calendar.set(Calendar.SECOND, 0);  
        calendar.set(Calendar.MILLISECOND, 0);  
    }  
      
    private static void setMaxTime(Calendar calendar){  
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));  
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));  
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));  
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));  
    }  
    
    
    public static void main(String[] args) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String a = "2018-01-18 00:18:00";
        try {
            Date d = f.parse(a);
            System.out.println(getPastHoursAgoTime(d,1));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
}
